package com.yangyang.androidconnect.controller;

import android.app.ActivityManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.yangyang.androidconnect.MyForegroundService;
import com.yangyang.androidconnect.common.ApiResult;
import com.yanzhenjie.andserver.annotation.GetMapping;
import com.yanzhenjie.andserver.annotation.PostMapping;
import com.yanzhenjie.andserver.annotation.RequestBody;
import com.yanzhenjie.andserver.annotation.RequestMapping;
import com.yanzhenjie.andserver.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(path = "/api")
public class ClipboardController {


    // 新增方法：获取剪贴板内容
    @GetMapping(path = "/get-clipboard")
    public ApiResult getClipboard(Context context) {
        try {
            Context appContext = MyForegroundService.getAppContext();
            ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            if (clipboard.hasPrimaryClip()) {
                ClipData clipData = clipboard.getPrimaryClip();
                if (clipData != null && clipData.getItemCount() > 0) {
                    ClipData.Item item = clipData.getItemAt(0);
                    String text = item.getText().toString();
                    return ApiResult.ok("剪贴板内容获取成功", text != null ? text : "");
                }
            } else {
                return ApiResult.error("剪贴板内容不可读");
            }
            return ApiResult.ok("剪贴板为空或内容不可读", "");
        } catch (Exception e) {
            return ApiResult.error("获取剪贴板内容失败: " + e.getMessage());
        }
    }

    // 新增方法：设置剪贴板内容
    @PostMapping(path = "/set-clipboard")
    public ApiResult setClipboard(@RequestBody JSONObject data, Context context) {

        if (!isAppInForeground(context)) {
            return ApiResult.error(403, "应用未在前台，无法修改剪切板");
        }
        String text = data.getString("content");
        try {
            ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("API设置", text);
            clipboard.setPrimaryClip(clip);
            return ApiResult.ok("剪贴板内容设置成功");
        } catch (Exception e) {
            return ApiResult.error("设置剪贴板内容失败: " + e.getMessage());
        }
    }


    // 在剪切板操作前添加可见性检查
    private boolean isAppInForeground(Context appContext) {
        ActivityManager am = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
        if (processes != null) {
            for (ActivityManager.RunningAppProcessInfo process : processes) {
                if (process.processName.equals(appContext.getPackageName())) {
                    return process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
                }
            }
        }
        return false;
    }

}
